import Vue from 'vue'
import axios from 'axios'
import {OPEN_AUTH_URL} from '@/constants'
import { HOST } from '@/constants'
import {storage} from '@/utils/tools'
import md5 from "js-md5";
import {  Loading } from 'element-ui';


var loadingQueue = [];
var loading_ui = null;
var showLoading = function(title='') {
  if(loadingQueue.length == 0) {
    loading_ui = Loading.service({
      lock: true,
      text: title?title:'请稍等...',
      spinner: 'el-icon-loading',
      background: 'rgba(204, 204, 204, 0.7)'
    });
  }
  loadingQueue.push("loading");
};
var hideLoading = function() {
  loadingQueue.pop();
  if(loadingQueue.length == 0) {
    Vue.nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
      loading_ui.close();
    });

  }
};

// var openid  = localStorage.getItem('WX_UID') || ''
// var openToken  = md5('renshe_member_'+openid)
// var myphone = localStorage.getItem('myphone') || ''
const Axios={}
Axios.post = function (url,data={}) {

  return new Promise((resolve,reject)=> {
    // data.openid = openid;
  data.token = 'aa08769cdcb26674c6706093503ff0a3';
  // data.phonetoken = myphone
  if(!data.cmloading){
    // Indicator.open()
    showLoading(data.loadTitle);
  }else{
    delete data['cmloading']
  }

  $.ajax({
    //请求方式
    type : "POST",
    //请求的媒体类型
    // contentType: "application/x-www-form-urlencoded",
    //请求地址
    url : HOST + url,
    timeout:0,
    //数据，json字符串
    data:data,
    dataType:'json',
    //请求成功
    success : function(result) {
      // document.getElementById('loading').style = 'none'
      setTimeout(()=>{
        // Indicator.close()
        hideLoading();
    },500)

      resolve(result)
    },
    //请求失败，包含具体的错误信息
    error : function(e){
      setTimeout(()=>{
        // Indicator.close()
        hideLoading();
    },500)

      reject(e)
    }
  });

})


}


Axios.postdq = function (url,data={}) {

  return new Promise((resolve,reject)=> {
    // data.openid = openid;
    data.token = 'aa08769cdcb26674c6706093503ff0a3';
    // data.phonetoken = myphone
    if(!data.cmloading){
      // Indicator.open()
      showLoading(data.loadTitle);
    }else{
      delete data['cmloading']
    }

    $.ajax({
      //请求方式
      type : "POST",
      //请求的媒体类型
      // contentType: "application/x-www-form-urlencoded",
      //请求地址
      url : HOST + url,
      timeout:200000,
      //数据，json字符串
      data:data,
      dataType:'json',
      //请求成功
      success : function(result) {
        // document.getElementById('loading').style = 'none'
        setTimeout(()=>{
          // Indicator.close()
          hideLoading();
        },500)

        resolve(result)
      },
      //请求失败，包含具体的错误信息
      error : function(e){
        setTimeout(()=>{
          // Indicator.close()
          hideLoading();
        },500)

        reject(e)
      }
    });

  })


}
Axios.get = function (url,data={}) {

  return new Promise((resolve,reject)=> {
    // data.openid = openid;
    // data.token = openToken
    if(!data.cmloading){
      // Indicator.open()
      showLoading(data.loadTitle);
    }else{
      delete data['cmloading']
    }

    $.ajax({
    //请求方式
    type : "GET",
    //请求的媒体类型
    // contentType: "application/json;charset=UTF-8",
    //请求地址
    url : HOST + url,
    //数据，json字符串
    data:data,
    //请求成功
    success : function(result) {
      setTimeout(()=>{
        // Indicator.close()
        hideLoading();
      },500)
      resolve(result)

    },
    //请求失败，包含具体的错误信息
    error : function(e){
      setTimeout(()=>{
        // Indicator.close()
        hideLoading();
      },500)
      reject(e)

    }
  });

})


}




export default Axios
